Skip to content

Latest commit

 

History

History
28 lines (21 loc) · 1.1 KB

dropping-all-tables-in-schema.mdx

File metadata and controls

28 lines (21 loc) · 1.1 KB
title description footerHelpType
Drop all tables in a PostgreSQL schema
Useful snippet for deleting all tables in a given schema
postgres

Execute the following query to drop all tables in a given schema. Replace my-schema-name with the name of your schema. In Supabase, the default schema is public.

This deletes all tables and their associated data. Ensure you have a recent backup before proceeding.

do $$ declare
    r record;
begin
    for r in (select tablename from pg_tables where schemaname = 'my-schema-name') loop
        execute 'drop table if exists ' || quote_ident(r.tablename) || ' cascade';
    end loop;
end $$;

This query works by listing out all the tables in the given schema and then executing a drop table for each (hence the for... loop).

You can run this query using the SQL Editor in the Supabase Dashboard, or via psql if you're connecting directly to the database.